package com.financing.wap.web.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.FpShopUserBackgroundDTO;
import com.financing.wap.dto.ShopperUserInfoDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpShopUserBackgroundService;
import com.financing.wap.service.IShopperUserInfoService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("shopperUserInfoController")
@RequestMapping("/api/shopperUserInfo")
public class ShopperUserInfoController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IShopperUserInfoService shopperUserInfoService;
	@Autowired
	private IFpShopUserBackgroundService fpShopUserBackgroundService;
	
	
	@RequestMapping("/detailShopperUserInfo")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail shopperUserInfo  begin .......");
			UserDTO user = SessionUtil.getSession().getUser();
			 ShopperUserInfoDTO shopperUserInfo=new ShopperUserInfoDTO();
			//用户id
			shopperUserInfo.setUserId(user.getId());
			ShopperUserInfoDTO dto = shopperUserInfoService.queryListGetFirst(shopperUserInfo);
			result.setAttribute("className", ShopperProductController.class.getName());
			result.setAttribute("methodName", "shopperproduct");
			result.setAttribute("dto",dto);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail shopperUserInfo  end .......");
		}catch(Exception e){
			log.error("query detail shopperUserInfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	@RequestMapping("/updateShopperUserInfo")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, ShopperUserInfoDTO shopperUserInfo){
		ResultMap result = new ResultMap();
		try{
			logger.info("update shopperUserInfo  begin .......");
			//修改类型     1、 修改全部    2、修改头像    3、修改背景图
			String updateType=request.getParameter("updateType");
			if("2".equals(updateType)&&StringUtils.isBlank(shopperUserInfo.getUserHeadImg())){
				result.setStatus(STATUS.FAILE);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}else if("3".equals(updateType)&&StringUtils.isBlank(shopperUserInfo.getShopBackground())){
				result.setStatus(STATUS.FAILE);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			
			
			UserDTO user = SessionUtil.getSession().getUser();
			
			//查询店招是否修改
			if(StringUtils.isNotBlank(shopperUserInfo.getShopBackground())){
				ShopperUserInfoDTO shopperDTO = new ShopperUserInfoDTO();
				shopperDTO.setUserPhone(user.getLoginName());
				List<ShopperUserInfoDTO> shopperList = shopperUserInfoService.queryList(shopperDTO);
				if(shopperList.size()==1){
					FpShopUserBackgroundDTO fp = new FpShopUserBackgroundDTO();
					fp.setDeleteTag("1");
					if(shopperList.get(0).getShopBackground()==null){
						fp.setLoginName(user.getLoginName());
						fp.setBackgroudImg(shopperUserInfo.getShopBackground());
						fpShopUserBackgroundService.insert(fp);
					}else if(!shopperList.get(0).getShopBackground().equals(shopperUserInfo.getShopBackground())){
						
						fp.setLoginName(user.getLoginName());
						fp.setBackgroudImg(shopperUserInfo.getShopBackground());
						fpShopUserBackgroundService.insert(fp);
					}
				}
			}
			
			
			
			//用户id
			shopperUserInfo.setUserId(user.getId());
			shopperUserInfo.setUserPhone(user.getLoginName());
			DTOdateSetUtil.setUpdateInfo(shopperUserInfo);
			shopperUserInfoService.updateSynchronized(shopperUserInfo,updateType);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("update shopperUserInfo list end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("update shopperUserInfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
}
